package vn.gq.udv.comic;

import vn.gq.udv.comic.model.item.Bookmark;
import vn.gq.udv.comic.model.item.ChapterItem;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class BookmarkHelper {
	
	public static final String PREF_BOOKMARK = "pref_bookmark";
	
	private SharedPreferences sharePref;
	
	public BookmarkHelper(Context context) {
		sharePref = context.getSharedPreferences(PREF_BOOKMARK, Context.MODE_PRIVATE);
	}
	
	public void saveBookmark(ChapterItem item, int position) {
		Editor editor = sharePref.edit();
		Bookmark bookmark = new Bookmark(item, position);
		editor.putString("pref_lastchapter", bookmark.toJSONObject().toString());
		editor.commit();
	}
	
	public Bookmark loadBookmark() {
		String json = sharePref.getString("pref_lastchapter", null);
		return json == null ? null : new Bookmark(json);
	}
	
	public boolean hasBookmark() {
		Bookmark bm = loadBookmark();
		if (bm != null) {
			if (!bm.getTitle().equals("") && (!bm.getChapterId().equals(""))) {
				return true;
			}
		}
		return false;
	}
	
	
	public static BookmarkHelper newInstance(Context context) {
		BookmarkHelper bm = new BookmarkHelper(context);
		return bm;
	}
	
}
